package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <dd>描述: ~ RoleServiceImpl</dd>
 * <dd>创建时间：  21:23 2020/10/8</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2020/10/8     yuzhiguo         1.0        1.0 Version
 */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data：{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Role> page= roleDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Transactional
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-添加]data:{},permissionIds:{},menuIds:{}", role, permissionIds,menuIds);
        //插入基本数据
        roleDao.insert(role);
        //逐条插入关系表数据
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }
        for (Integer menuId : menuIds) {
            roleDao.insertRoleAndMenu(role.getId(),menuId);
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        log.info("[角色-根据id查询]id:{}", id);
        return roleDao.findById(id);
    }

    /**
     * 根据角色ID查询关联的权限ID集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        log.info("[角色-根据角色ID查询关联的权限ID集合]id:{}", id);
        return roleDao.findPermissionIdsByRoleId(id);
    }

    /**
     * 根据角色ID查询关联的菜单ID集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        log.info("[角色-根据角色ID查询关联的菜单ID集合]id:{}", id);
        return roleDao.findMenuIdsByRoleId(id);
    }

    /**
     * 编辑
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-编辑]data:{},permissionIds:{},menuIds:{}", role, permissionIds,menuIds);
        //更新角色
        roleDao.update(role);
        //更新关联关系
        Long countByRoleAndPermission = roleDao.selectCountByRoleAndPermission(role.getId());
        //删除原有角色和权限的关系
        if (countByRoleAndPermission>0) {
            roleDao.deleteRoleAndPermissionByRoleId(role.getId());
        }
        //加入当前角色和权限的关系
        for (Integer permissionId : permissionIds) {
            roleDao.insertRoleAndPermission(role.getId(),permissionId);
        }

        Long countByRoleAndMenu = roleDao.selectCountByRoleAndMenu(role.getId());
        //删除原有角色和菜单的关系
        if (countByRoleAndMenu>0) {
            roleDao.deleteRoleAndMenuByRoleId(role.getId());
        }
        //加入当前角色和菜单的关系
        for (Integer menuId : menuIds) {
            roleDao.insertRoleAndMenu(role.getId(),menuId);
        }
    }

    /**
     * 根据id删除角色
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //根据roleId删除关联权限表
        roleDao.deleteRoleAndPermissionByRoleId(id);
        //根据roleId删除关联角色表
        roleDao.deleteRoleAndMenuByRoleId(id);
        //根据id删除
        roleDao.deleteById(id);
    }
}
